﻿@inherits BaseComponent

@if (Visible)
{
    <div @ref="container" class="kui-upload-field" @onpaste="OnPasteFileAsync">
        @if (IsDrag) {
            <div class="kui-upload ant-upload ant-upload-select-text ant-upload-drag ant-upload-select">
                <div class="ant-upload" style="display:grid;">
                    @if (!ReadOnly) {
                        @BuildInput
                    }
                    <div class="ant-upload-drag-container">
                        <p class="ant-upload-drag-icon"><Icon Type="inbox" /></p>
                        <p class="ant-upload-text">@Language[Language.DragFileUpload]</p>
                        <p class="ant-upload-hint">@Language[HintText]</p>
                    </div>
                </div>
            </div>
        } else if (IsButton) {
            if (!ReadOnly) {
                <div class="kui-upload">
                    <div class="kui-button">
                        <KIcon Icon="upload" />
                        <span>@Language[Language.Upload]</span>
                        @BuildInput
                    </div>
                    @BuildTemplate
                </div>
            }
        } else {
            if (!ReadOnly) {
                <div class="kui-upload">
                    @BuildInput
                    @BuildTemplate
                </div>
            }
        }
        @if (isAdding) {
            <div class="ant-btn-link">@Language[Language.Loading]</div>
        }
        @if (sysFiles != null && sysFiles.Count > 0) {
            <div class="kui-form-files">
                @foreach (var item in sysFiles) {
                    var url = item.FileUrl;
                    <div>
                        @if (!ReadOnly && IsDelete) {
                            <span class="kui-link kui-danger" @onclick="e=>OnDeleteFileAsync(item)">@Language[Language.Delete]</span>
                        }
                        <KFileLink Item="item" />
                        @if (!string.IsNullOrWhiteSpace(item.Id)) {
                            <KIcon Icon="file" Title="@Language.PreviewFile" OnClick="this.Callback<MouseEventArgs>(e => OnShowFile(item))" />
                        }
                    </div>
                }
            </div>
        }
    </div>
}

@code {
    private RenderFragment BuildInput => @<div>
        <InputFile multiple="@IsMultiple" webkitdirectory="@Directory" accept="@Accept" disabled="@(!Enabled)" OnChange="OnInputFileChangedAsync" @attributes="InputAttributes" />
    </div>;

    private RenderFragment BuildTemplate => @<div class="template">
        @if (!string.IsNullOrWhiteSpace(TemplateUrl)) {
            <KAnchor Href="@TemplateUrl" Download="">@Language[Language.Template]</KAnchor>
        }
    </div>;
}